import com.arcadedb.database.Database;
import com.arcadedb.database.DatabaseFactory;
import com.arcadedb.graph.MutableEdge;
import com.arcadedb.graph.MutableVertex;
import com.arcadedb.graph.Vertex;
import com.arcadedb.query.sql.executor.Result;
import com.arcadedb.query.sql.executor.ResultSet;
import indi.hjhk.log.Logger;

public class QueryTest {
    public static void main(String[] args){
        Logger logger = Logger.getPureLogger();
        logger.logSilent("start");
        DatabaseFactory dbFactory= new DatabaseFactory("./databases/test5");
        try(Database db=dbFactory.open()) {

//        db.begin();
//        MutableVertex v1=db.newVertex("SnapShotTest");
//        MutableVertex v2=db.newVertex("SnapShotTest");
//        v1.set("id", "case1");
//        v1.set("carId", null);
//        v1.set("predId", null);
//        v1.set("succId", "case1");
//        v1.set("averageSpeed", 0);
//        v1.set("currentSpeed", 0);
//        v1.set("lastPeriodTime", 0);
//        v1.set("lastPeriodMinMile", 0);
//        v1.set("TimeExpired", 0);
//        v1.set("timestamp", 0);
//        v1.set("expired", 123123123);
//        v1.set("passid", "case1");
//        v1.save();
//        v2.set("id", "case1");
//        v2.set("carId", null);
//        v2.set("predId", null);
//        v2.set("succId", "case1");
//        v2.set("averageSpeed", 0);
//        v2.set("currentSpeed", 0);
//        v2.set("lastPeriodTime", 0);
//        v2.set("lastPeriodMinMile", 0);
//        v2.set("TimeExpired", 0);
//        v2.set("timestamp", 1231231234);
//        v2.set("expired", 0);
//        v2.set("passid", "case1");
//        v2.save();
//        v1.newEdge("succ",v2,true);
//        db.commit();

            long start = System.currentTimeMillis();
            ResultSet rs = db.query("SQL", "MATCH {TYPE: SnapShot, AS: pred}.out('succ') {AS: succ, WHERE: ($matched.pred.expired<=timestamp)}" +
//                    " RETURN pred, succ");
                    " RETURN pred.id AS pred_id, pred.expired AS pred_expired, succ.id AS succ_id, succ.timestamp AS succ_timestamp");
            logger.logOnStdout("SQL executed, printing results...");

            logger.disableAutoFlush();
            int count=0;
            while (rs.hasNext()){
                count++;
                Result result=rs.next();
//                Vertex vPred=result.getProperty("pred");
//                Vertex vSucc=result.getProperty("succ");
//                logger.logSilent("%s(expired=%d) -> %s(timestamp=%d)", vPred.get("id"), vPred.get("expired"), vSucc.get("id"), vSucc.get("timestamp"));
                logger.logSilent("%s(expired=%d) -> %s(timestamp=%d)", result.getProperty("pred_id"), result.getProperty("pred_expired"),
                        result.getProperty("succ_id"), result.getProperty("succ_timestamp"));
            }
            logger.flush();
            logger.enableAutoFlush();

            long elapsed = System.currentTimeMillis() - start;
            logger.logOnStdout(count+" results printed ,total time used: "+elapsed + "ms");
        }
    }
}
